package com.jd.controller;

import com.jd.enums.YesOrNo;
import com.jd.pojo.Carousel;
import com.jd.pojo.Category;
import com.jd.service.CarouselService;
import com.jd.service.CategoryService;
import com.jd.utils.JDJSONResult;
import com.jd.vo.CategoryVO;
import com.jd.vo.NewItemsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "首页",tags = {"首页展示相关接口"})
@RestController
@RequestMapping("index")
public class IndexController {

    @Autowired
    private CarouselService carouselService;


    @Autowired
    private CategoryService categoryService;


    @ApiOperation(value = "获取首页轮播图列表",notes = "获取首页轮播图列表",httpMethod = "GET")
    @GetMapping("/carousel")
    public JDJSONResult carousel(){
        List<Carousel> list=carouselService.queryAll(YesOrNo.YES.type);
        return JDJSONResult.ok(list);
    }


    /**
     * 首页分离展示需求
     * 1 第一次刷新主页查询大分类，渲染展示到首页
     * 2、如果鼠标移动到大分类，则加载器子分类的内容
     */


    @ApiOperation(value = "获取商品分类（一级分类）",notes = "获取商品分类（一级分类",httpMethod = "GET")
    @GetMapping("/cats")
    public JDJSONResult cats(){
        List<Category> list=categoryService.queryAllRootLevelCat();
        return JDJSONResult.ok(list);
    }



    /**
     * 首页分离展示需求
     * 1 第一次刷新主页查询大分类，渲染展示到首页
     * 2、如果鼠标移动到大分类，则加载器子分类的内容
     */


    @ApiOperation(value = "获取商品子分类",notes = "获取商品子分类",httpMethod = "GET")
    @GetMapping("/subCat/{rootCatId}")
    public JDJSONResult subCat(
            @ApiParam(name = "rootCatId",value = "一级分类id",required = true)
            @PathVariable Integer rootCatId){
        if(rootCatId==null){
            return JDJSONResult.errorMsg("分类不存在");
        }
        List<CategoryVO> list=categoryService.getSubCatList(rootCatId);
        return JDJSONResult.ok(list);
    }


    @ApiOperation(value = "查询每个一级分类下的最新6条商品数据",notes = "查询每个一级分类下的最新6条商品数据",httpMethod = "GET")
    @GetMapping("/sixNewItems/{rootCatId}")
    public JDJSONResult sixNewItems(
            @ApiParam(name = "rootCatId",value = "一级分类id",required = true)
            @PathVariable Integer rootCatId){
        if(rootCatId==null){
            return JDJSONResult.errorMsg("分类不存在");
        }
        List<NewItemsVO> list=categoryService.getSixNewItemsLazy(rootCatId);
        return JDJSONResult.ok(list);
    }



}
